package common

import (
	"errors"
	"github.com/gin-gonic/gin"
	"menu-generation-go/models"
)

// GetUserSession 从 Gin 上下文中获取用户会话
func GetUserSession(c *gin.Context) (*models.UserSession, error) {
	session, exists := c.Get("user_session")
	if !exists {
		return nil, errors.New("用户会话不存在")
	}

	userSession, ok := session.(models.UserSession)
	if !ok {
		return nil, errors.New("无效的用户会话类型")
	}

	return &userSession, nil
}

// GetUserID 从 Gin 上下文中获取用户ID
func GetUserID(c *gin.Context) (uint, error) {
	userID, exists := c.Get("user_id")
	if !exists {
		return 0, errors.New("用户ID不存在")
	}

	id, ok := userID.(uint)
	if !ok {
		return 0, errors.New("无效的用户ID类型")
	}

	return id, nil
}
